import Vue from 'vue'
import VueRouter from 'vue-router'
import Error from "@/components/Error.vue";
import Login from "@/views/Login.vue";
import Index from "@/views/Index.vue";
import ManagerHome from "@/views/ManagerHome.vue";
import UserManager from "@/views/UserManager.vue";
import UserUpdate from "@/views/UserUpdate.vue";
import Register from "@/views/Register.vue";
import ProjectForm from "@/views/ProjectForm.vue";
import ResetPassword from "@/views/ResetPassword.vue";
import UpdateOneself from "@/views/UpdateOneself.vue";
import Announcement from "@/views/Announcement.vue";
import NoticesManager from "@/views/NoticesManager.vue";
import NoticesUpdate from "@/views/NoticesUpdate.vue";
import NoticesAdd from "@/views/NoticesAdd.vue";
import ProjectManager from "@/views/ProjectManager.vue";
import projectManager from "@/views/ProjectManager.vue";
import ProjectUpdate from "@/views/ProjectUpdate.vue";
import PdfManager from "@/views/PdfManager.vue";

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: '客户端首页',
        component: Index,
        children: [

            {
                path: '/projectForm',
                name: '表单',
                component: ProjectForm
            },
            {
                path: '/updateOneself',
                name: '修改信息',
                component: UpdateOneself
            },
            {
                path: '/announcement',
                name: '公告',
                component: Announcement
            },


        ]

    },
    {
        path: '/login',
        name: '登录',
        component: Login
    },
    {
        path: '/register',
        name: '注册',
        component: Register
    },
    {
        path: '/resetPassword',
        name: '重置密码',
        component: ResetPassword
    },


    {
        path: '/error',
        name: '登录错误',
        component: Error
    },
    {
        path: '/manager',
        name: '管理端首页',
        component: ManagerHome,
        children: [
            {
                path: '/userManager',
                name: '用户管理',
                component: UserManager
            },
            {
                path: '/userUpdate',
                name: '用户修改',
                component: UserUpdate
            },
            {
                path: '/noticesManager',
                name: '公告管理',
                component: NoticesManager
            },
            {
                path: '/noticesUpdate',
                name: '公告修改',
                component: NoticesUpdate
            },
            {
                path: '/noticesAdd',
                name: '添加公告',
                component: NoticesAdd
            },
            {

                path: '/projectManager',
                name: '项目管理',
                component: ProjectManager
            },
            {
                path: '/projectUpdate',
                name: '项目修改',
                component: ProjectUpdate
            },
            {
                path: '/pdfManager',
                name: '文件管理',
                component: PdfManager
            }
        ]
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

router.beforeEach((to, from, next) => {
    if (to.path === '/' || to.path === '/Register' || to.path === '/register' || to.path === '/ResetPassword' || to.path === '/Announcement') {
        next()
    } else if (to.path.startsWith('/login')) {
        window.localStorage.removeItem('access-admin')
        next() //
    } else {
        let admin = JSON.parse(window.localStorage.getItem('access-admin'))
        if (!admin) {

            next('/login')

        } else {
            //校验token合法性
            axios({
                url: 'http://localhost:8082/user/checkToken',
                method: 'get',
                headers: {
                    token: admin.token,
                    adminId: admin.id
                }
            }).then((response) => {
                console.log(response.data)
                if (!response.data) {
                    console.log('校验失败')
                    next({path: '/error'})
                }
            })
            next()
        }
    }
})

export default router